package com.ruge.seckill.domain.dao;

import com.ruge.seckill.domain.model.SeckillCoupon;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * @author ruge.wu
 * @since 2022/1/26 15:54
 */
public interface SeckillCouponRepository extends JpaRepository<SeckillCoupon, Long> {
    @Modifying
    @Transactional
    @Query(value = "update SeckillCoupon set amount = amount - 1 where couponId = :couponId")
    void updateAmountBySecKill(@Param("couponId") String couponId);

    Optional<SeckillCoupon> findFirstByCouponId(String couponId);

    Optional<SeckillCoupon> findFirstByCouponIdAndIsValidTrue(String couponId);
}
